﻿using Microsoft.AspNetCore.Authentication;
using OpenIddict.Abstractions;

namespace Cordyceps.Micro.AuthServer;

public class AuthServer
{
    public bool IsAuthenticated(AuthenticateResult authenticateResult, OpenIddictRequest request)
    {
        if (!authenticateResult.Succeeded) return false;
        if (request.MaxAge.HasValue && authenticateResult.Properties != null)
        {
            var maxAgeSecondes = TimeSpan.FromSeconds(request.MaxAge ?? 0);
            var expired = !authenticateResult.Properties.IssuedUtc.HasValue ||
                          authenticateResult.Properties.IssuedUtc.Value + maxAgeSecondes < DateTimeOffset.UtcNow;
            if (expired) return false;
        }

        return true;
    }
}